import { computed, reactive, ref } from "vue";
import { createGlobalState } from "@vueuse/core";
import type { UserLoginDto } from "@/api";
import { Client } from "@/api/clientApi";

export enum FormStatus {
  LoginForm,
  RegisterForm,
}

export const useLoginForm = createGlobalState(() => {
  const formStats = ref<FormStatus>(FormStatus.LoginForm);

  const getStatus = computed(() => formStats.value);

  function setStatus(status: FormStatus) {
    formStats.value = status;
  }

  return { formStats, getStatus, setStatus };
});

// export const useLogin = () => {
//   const { setStatus } = useLoginForm();
//
//   const formState = reactive<UserLoginDto>({
//     username: "",
//     password: "",
//     remember: false,
//   });
//   function switchLogin() {
//     setStatus(FormStatus.LoginForm);
//   }
//
//   async function login() {
//     // 登录
//     const res = await Client.systemSysUser.postSysUserLogin(formState);
//     localStorage.setItem("user", JSON.stringify(res.sysUserVo));
//     localStorage.setItem("token", res.token);
//   }
//
//   return { switchLogin, formState, login };
// };

// export const useRegister = () => {
//   const { setStatus } = useLoginForm();
//
//   function switchRegister() {
//     setStatus(FormStatus.RegisterForm);
//   }
//   return { switchRegister };
// };
